package com.ithengxing.d3_udp2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ServerUdp {
    public static void main(String[] args) throws Exception {
        System.out.println("===========服务端启动=============");
        //1.创建接收对象（人）注册端口
        DatagramSocket sc = new DatagramSocket(8888);

        //创建一个数据包对象接收客户端传输过来的数据
        byte[] bytes = new byte[1024 * 60];
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length);

        while (true) {
            // 3、等待接收数据。
            sc.receive(packet);
            // 4、取出数据即可
            // 读取多少倒出多少
            int len = packet.getLength();
            String rs = new String(bytes ,0, len);
            System.out.println("收到了来自：" + packet.getAddress() +", 对方端口是" + packet.getPort() +"的消息：" + rs);
        }

    }
}
